package com.jzo2o.customer.service.impl;

import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务人员认证审核表 服务实现类
 * </p>
 *
 * @author Echo
 * @since 2025-03-08
 */
@Service
@RequiredArgsConstructor
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements IWorkerCertificationAuditService {
    private final WorkerCertificationMapper workerMapper;
    /**
     * 分页查询服务人员认证信息
     * @param dto
     * @return
     */
    @Override
    public PageResult<WorkerCertificationAuditResDTO> pageQueryWorker(WorkerCertificationAuditPageQueryReqDTO dto) {
        PageResult<WorkerCertificationAuditResDTO> result = PageHelperUtils.selectPage(dto,
                () -> baseMapper.pageQueryWorker(dto));
        if (CollUtils.isEmpty(result.getList())) {
            return PageResult.empty(0L, 0L);
        }
        return result;
    }

    /**
     * 审核服务人员的认证申请
     * @param id
     * @param dto
     */
    @Override
    public void auditWorker(Long id, CertificationAuditReqDTO dto) {
        WorkerCertificationAudit audit = getById(id);
        if (ObjectUtils.isNull(audit)) {
            throw new DBException("认证信息不存在！");
        }
        Integer certificationStatus = dto.getCertificationStatus();
        String rejectReason = dto.getRejectReason();
        boolean success = lambdaUpdate()
                .eq(WorkerCertificationAudit::getId, id)
                .set(certificationStatus != null, WorkerCertificationAudit::getCertificationStatus, certificationStatus)
                .set(rejectReason != null, WorkerCertificationAudit::getRejectReason, rejectReason)
                // 修改审核状态为已审核
                .set(WorkerCertificationAudit::getAuditStatus, 1)
                .set(WorkerCertificationAudit::getAuditorId, UserContext.currentUserId())
                .set(WorkerCertificationAudit::getAuditorName, UserContext.currentUser().getName())
                .set(WorkerCertificationAudit::getAuditTime, LocalDateTime.now())
                .update();
        if (!success) {
            throw new DBException("审核认证信息失败");
        }
        // 更新服务人员认证信息
        WorkerCertification certification = workerMapper.selectById(audit.getServeProviderId());
        certification.setCertificationStatus(certificationStatus);
        certification.setCertificationTime(LocalDateTime.now());
        workerMapper.updateById(certification);
    }

    /**
     * 服务人员提交认证申请后，需要保存认证信息到审核表中
     * @param dto
     */
    @Override
    public void saveCertification(WorkerCertificationAuditAddReqDTO dto) {
        WorkerCertificationAudit certificationAudit = BeanUtils.copyBean(dto, WorkerCertificationAudit.class);
        // 设置审核状态为待审核，认证状态为认证中
        certificationAudit.setAuditStatus(0);
        certificationAudit.setCertificationStatus(CertificationStatusEnum.PROGRESSING);
        save(certificationAudit);
    }
}
